/**
  ******************************************************************************
  * File Name          : envirctrl.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include <string.h>
#include "network.h"
#include "route.h"

static Network_JsonPointerType jsp;
static char valuebuf[128];

static uint8_t route_ec_up_collector(Route_PayloadType *rtpl)
{
	Collector_DataType *co = NULL;
  
  if((rtpl == NULL) || (rtpl->pl_len != sizeof(Collector_DataType)) )
  {
    return 1;
  }
  
  co = (Collector_DataType*)(rtpl->payload);
  
  memset(&jsp, 0, sizeof(Network_JsonPointerType));
  memset(valuebuf, 0, sizeof(valuebuf));
  
  jsp.property = cJSON_CreateObject();
  
	snprintf(valuebuf, 32, "%d", co->temperature);
  jsp.js_value = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "t", jsp.js_value);
	
	snprintf(valuebuf, 32, "%u", co->humidity);
  jsp.js_value = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "rh", jsp.js_value);
	
	snprintf(valuebuf, 32, "%u", co->nh3);
  jsp.js_value = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "nh3", jsp.js_value);
	
	snprintf(valuebuf, 32, "%d", co->tvoc);
  jsp.js_value = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "voc", jsp.js_value);
	
	snprintf(valuebuf, 32, "%d", co->co2);
  jsp.js_value = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "co2", jsp.js_value);
	
	snprintf(valuebuf, 32, "%d", co->pressure);
  jsp.js_value = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "fy", jsp.js_value);
	
  
  jsp.identity = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.identity, "productID", Route_str_pids[RT_DV_HK]);
  cJSON_AddStringToObject(jsp.identity, "deviceName", Route_DeviceList.hk.did);
  
  jsp.arrayitem = cJSON_CreateObject();
  cJSON_AddItemToObject(jsp.arrayitem, "identity", jsp.identity);
  cJSON_AddItemToObject(jsp.arrayitem, "properties", jsp.property);
  
  jsp.params = cJSON_CreateArray();
  cJSON_AddItemToArray(jsp.params, jsp.arrayitem);
  
  jsp.root = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.root, "id", "1");
  cJSON_AddStringToObject(jsp.root, "version", ONEJSON_VERSION);
  cJSON_AddItemToObject(jsp.root, "params", jsp.params);
  
  cJSON_PrintPreallocated(jsp.root, Network_Pub_payload, NETWORK_PUB_PAYLOAD_BUFFER_MAXLEN, 0);
  
	snprintf(Network_Pub_topic, NETWORK_PUB_TOPIC_BUFFER_MAXLEN, Route_str_Pub_Pack_Post, ROUTE_SELF_DID);
	
  cJSON_Delete(jsp.js_value);
  cJSON_Delete(jsp.root);
  cJSON_Delete(jsp.params);
  cJSON_Delete(jsp.arrayitem);
  cJSON_Delete(jsp.identity);
  cJSON_Delete(jsp.property);
	
	return 0;
}

static uint8_t route_ec_up_st_fn(Route_PayloadType *rtpl)
{
	uint8_t i;
	Ctrl_StateFNType *fanst = NULL;
  
  if( (rtpl == NULL) || (rtpl->pl_len != sizeof(Ctrl_StateFNType)) )
  {
    return 1;
  }
  
  fanst = (Ctrl_StateFNType*)(rtpl->payload);
  
  memset(&jsp, 0, sizeof(Network_JsonPointerType));
  memset(valuebuf, 0, sizeof(valuebuf));
  
  jsp.property = cJSON_CreateObject();
  
	/* 风机状态 */
	for(i=0; i<CTR_FAN_CHANNEL_ALLNUM; i++)
	{
		if(Ctr_CheckStateCorrect(fanst->fnst[i]) != 0)
		{
			valuebuf[2*i] = 'N';
		}
		else
		{
			valuebuf[2*i] = fanst->fnst[i];
		}
		valuebuf[(2*i) + 1] = '-';
	}
	valuebuf[2*i] = '\0';
  jsp.js_value = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "fnst", jsp.js_value);
	
  
  jsp.identity = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.identity, "productID", Route_str_pids[RT_DV_HK]);
  cJSON_AddStringToObject(jsp.identity, "deviceName", Route_DeviceList.hk.did);
  
  jsp.arrayitem = cJSON_CreateObject();
  cJSON_AddItemToObject(jsp.arrayitem, "identity", jsp.identity);
  cJSON_AddItemToObject(jsp.arrayitem, "properties", jsp.property);
  
  jsp.params = cJSON_CreateArray();
  cJSON_AddItemToArray(jsp.params, jsp.arrayitem);
  
  jsp.root = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.root, "id", "1");
  cJSON_AddStringToObject(jsp.root, "version", ONEJSON_VERSION);
  cJSON_AddItemToObject(jsp.root, "params", jsp.params);
  
  cJSON_PrintPreallocated(jsp.root, Network_Pub_payload, NETWORK_PUB_PAYLOAD_BUFFER_MAXLEN, 0);
  
	snprintf(Network_Pub_topic, NETWORK_PUB_TOPIC_BUFFER_MAXLEN, Route_str_Pub_Pack_Post, ROUTE_SELF_DID);
	
  cJSON_Delete(jsp.js_value);
  cJSON_Delete(jsp.root);
  cJSON_Delete(jsp.params);
  cJSON_Delete(jsp.arrayitem);
  cJSON_Delete(jsp.identity);
  cJSON_Delete(jsp.property);
	
	return 0;
}

static uint8_t route_ec_up_st_sl(Route_PayloadType *rtpl)
{
	uint8_t i;
	Ctrl_StateSLType *shlst = NULL;
  
  if( (rtpl == NULL) || (rtpl->pl_len != sizeof(Ctrl_StateSLType)) )
  {
    return 1;
  }
  
  shlst = (Ctrl_StateSLType*)(rtpl->payload);
  
  memset(&jsp, 0, sizeof(Network_JsonPointerType));
  memset(valuebuf, 0, sizeof(valuebuf));
  
  jsp.property = cJSON_CreateObject();
	
	/* 水帘状态 */
	for(i=0; i<CTR_SHL_CHANNEL_ALLNUM; i++)
	{
		if(Ctr_CheckStateCorrect(shlst->slst[i]) != 0)
		{
			valuebuf[2*i] = 'N';
		}
		else
		{
			valuebuf[2*i] = shlst->slst[i];
		}
		valuebuf[(2*i) + 1] = '-';
	}
	valuebuf[2*i] = '\0';
  jsp.js_value = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "slst", jsp.js_value);
	
  
  jsp.identity = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.identity, "productID", Route_str_pids[RT_DV_HK]);
  cJSON_AddStringToObject(jsp.identity, "deviceName", Route_DeviceList.hk.did);
  
  jsp.arrayitem = cJSON_CreateObject();
  cJSON_AddItemToObject(jsp.arrayitem, "identity", jsp.identity);
  cJSON_AddItemToObject(jsp.arrayitem, "properties", jsp.property);
  
  jsp.params = cJSON_CreateArray();
  cJSON_AddItemToArray(jsp.params, jsp.arrayitem);
  
  jsp.root = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.root, "id", "1");
  cJSON_AddStringToObject(jsp.root, "version", ONEJSON_VERSION);
  cJSON_AddItemToObject(jsp.root, "params", jsp.params);
  
  cJSON_PrintPreallocated(jsp.root, Network_Pub_payload, NETWORK_PUB_PAYLOAD_BUFFER_MAXLEN, 0);
  
	snprintf(Network_Pub_topic, NETWORK_PUB_TOPIC_BUFFER_MAXLEN, Route_str_Pub_Pack_Post, ROUTE_SELF_DID);
	
  cJSON_Delete(jsp.js_value);
  cJSON_Delete(jsp.root);
  cJSON_Delete(jsp.params);
  cJSON_Delete(jsp.arrayitem);
  cJSON_Delete(jsp.identity);
  cJSON_Delete(jsp.property);
	
	return 0;
}

static uint8_t route_ec_up_st_nq(Route_PayloadType *rtpl)
{
	uint8_t i;
	Ctrl_StateNQType *nuqst = NULL;
  
  if( (rtpl == NULL) || (rtpl->pl_len != sizeof(Ctrl_StateNQType)) )
  {
    return 1;
  }
  
  nuqst = (Ctrl_StateNQType*)(rtpl->payload);
  
  memset(&jsp, 0, sizeof(Network_JsonPointerType));
  memset(valuebuf, 0, sizeof(valuebuf));
  
  jsp.property = cJSON_CreateObject();
  
	/* 暖气状态 */
	for(i=0; i<CTR_NUQ_CHANNEL_ALLNUM; i++)
	{
		if(Ctr_CheckStateCorrect(nuqst->nqst[i]) != 0)
		{
			valuebuf[2*i] = 'N';
		}
		else
		{
			valuebuf[2*i] = nuqst->nqst[i];
		}
		valuebuf[(2*i) + 1] = '-';
	}
	valuebuf[2*i] = '\0';
  jsp.js_value = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "nqst", jsp.js_value);
	
  
  jsp.identity = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.identity, "productID", Route_str_pids[RT_DV_HK]);
  cJSON_AddStringToObject(jsp.identity, "deviceName", Route_DeviceList.hk.did);
  
  jsp.arrayitem = cJSON_CreateObject();
  cJSON_AddItemToObject(jsp.arrayitem, "identity", jsp.identity);
  cJSON_AddItemToObject(jsp.arrayitem, "properties", jsp.property);
  
  jsp.params = cJSON_CreateArray();
  cJSON_AddItemToArray(jsp.params, jsp.arrayitem);
  
  jsp.root = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.root, "id", "1");
  cJSON_AddStringToObject(jsp.root, "version", ONEJSON_VERSION);
  cJSON_AddItemToObject(jsp.root, "params", jsp.params);
  
  cJSON_PrintPreallocated(jsp.root, Network_Pub_payload, NETWORK_PUB_PAYLOAD_BUFFER_MAXLEN, 0);
  
	snprintf(Network_Pub_topic, NETWORK_PUB_TOPIC_BUFFER_MAXLEN, Route_str_Pub_Pack_Post, ROUTE_SELF_DID);
	
  cJSON_Delete(jsp.js_value);
  cJSON_Delete(jsp.root);
  cJSON_Delete(jsp.params);
  cJSON_Delete(jsp.arrayitem);
  cJSON_Delete(jsp.identity);
  cJSON_Delete(jsp.property);
	
	return 0;
}

static uint8_t route_ec_up_st_vf(Route_PayloadType *rtpl)
{
	Ctrl_StateVFType *vfdst = NULL;
  
  if( (rtpl == NULL) || (rtpl->pl_len != sizeof(Ctrl_StateVFType)) )
  {
    return 1;
  }
  
  vfdst = (Ctrl_StateVFType*)(rtpl->payload);
  
  memset(&jsp, 0, sizeof(Network_JsonPointerType));
  memset(valuebuf, 0, sizeof(valuebuf));
  
  jsp.property = cJSON_CreateObject();
	
	/* 变频器状态 */
	snprintf(valuebuf, 32, "%c#%d-%c#%d-%c#%d-%c#%d", vfdst->vfst[0], vfdst->vfjs[0], 
	                                                  vfdst->vfst[1], vfdst->vfjs[1], 
	                                                  vfdst->vfst[2], vfdst->vfjs[2],
	                                                  vfdst->vfst[3], vfdst->vfjs[3]);
  jsp.js_value = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "vfst", jsp.js_value);
	
  
  jsp.identity = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.identity, "productID", Route_str_pids[RT_DV_HK]);
  cJSON_AddStringToObject(jsp.identity, "deviceName", Route_DeviceList.hk.did);
  
  jsp.arrayitem = cJSON_CreateObject();
  cJSON_AddItemToObject(jsp.arrayitem, "identity", jsp.identity);
  cJSON_AddItemToObject(jsp.arrayitem, "properties", jsp.property);
  
  jsp.params = cJSON_CreateArray();
  cJSON_AddItemToArray(jsp.params, jsp.arrayitem);
  
  jsp.root = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.root, "id", "1");
  cJSON_AddStringToObject(jsp.root, "version", ONEJSON_VERSION);
  cJSON_AddItemToObject(jsp.root, "params", jsp.params);
  
  cJSON_PrintPreallocated(jsp.root, Network_Pub_payload, NETWORK_PUB_PAYLOAD_BUFFER_MAXLEN, 0);

	snprintf(Network_Pub_topic, NETWORK_PUB_TOPIC_BUFFER_MAXLEN, Route_str_Pub_Pack_Post, ROUTE_SELF_DID);
	
  cJSON_Delete(jsp.js_value);
  cJSON_Delete(jsp.root);
  cJSON_Delete(jsp.params);
  cJSON_Delete(jsp.arrayitem);
  cJSON_Delete(jsp.identity);
  cJSON_Delete(jsp.property);
	
	return 0;
}

static uint8_t route_ec_up_md(Route_PayloadType *rtpl)
{
  Ctrl_ParaMDType *md = NULL;
  
  if( (rtpl == NULL) || (rtpl->pl_len != sizeof(Ctrl_ParaMDType)) )
  {
    return 1;
  }
  
  md = (Ctrl_ParaMDType*)(rtpl->payload);
  
  memset(&jsp, 0, sizeof(Network_JsonPointerType));
  memset(valuebuf, 0, sizeof(valuebuf));
  
  jsp.property = cJSON_CreateObject();
  
  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 32, "%u#%u-%u-%u-%u", md->mode, md->fnmdsken, md->slmdsken, md->nqmdsken, md->vfmdsken);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "mode", jsp.js_value);
  
  
  jsp.identity = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.identity, "productID", Route_str_pids[RT_DV_HK]);
  cJSON_AddStringToObject(jsp.identity, "deviceName", Route_DeviceList.hk.did);
  
  jsp.arrayitem = cJSON_CreateObject();
  cJSON_AddItemToObject(jsp.arrayitem, "identity", jsp.identity);
  cJSON_AddItemToObject(jsp.arrayitem, "properties", jsp.property);
  
  jsp.params = cJSON_CreateArray();
  cJSON_AddItemToArray(jsp.params, jsp.arrayitem);
  
  jsp.root = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.root, "id", "1");
  cJSON_AddStringToObject(jsp.root, "version", ONEJSON_VERSION);
  cJSON_AddItemToObject(jsp.root, "params", jsp.params);
  
  cJSON_PrintPreallocated(jsp.root, Network_Pub_payload, NETWORK_PUB_PAYLOAD_BUFFER_MAXLEN, 0);

	snprintf(Network_Pub_topic, NETWORK_PUB_TOPIC_BUFFER_MAXLEN, Route_str_Pub_Pack_Post, ROUTE_SELF_DID);
	
  cJSON_Delete(jsp.js_value);
  cJSON_Delete(jsp.root);
  cJSON_Delete(jsp.params);
  cJSON_Delete(jsp.arrayitem);
  cJSON_Delete(jsp.identity);
  cJSON_Delete(jsp.property);
  
  return 0;
}

static uint8_t route_ec_up_jt(Route_PayloadType *rtpl)
{
  Ctrl_ParaJTType *jt = NULL;
  
  if( (rtpl == NULL) || (rtpl->pl_len != sizeof(Ctrl_ParaJTType)) )
  {
    return 1;
  }
  
  jt = (Ctrl_ParaJTType*)(rtpl->payload);
  
  memset(&jsp, 0, sizeof(Network_JsonPointerType));
  memset(valuebuf, 0, sizeof(valuebuf));
  
  jsp.property = cJSON_CreateObject();
  
  /* 风机 */
  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 32, "%d", jt->fnhitp);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "jtfnhitp", jsp.js_value);
  
  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 32, "%d", jt->fnlotp);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "jtfnlotp", jsp.js_value);
  
  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 32, "%u", jt->fnsptp);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "jtfnsptp", jsp.js_value);
  
  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 32, "%u", jt->fnrttp);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "jtfnrttp", jsp.js_value);
  
  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 32, "%u", jt->fnhin3);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "jtfnhin3", jsp.js_value);
  
  /* 氺帘 */
  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 32, "%d", jt->slhitp);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "jtslhitp", jsp.js_value);
  
  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 32, "%d", jt->sllotp);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "jtsllotp", jsp.js_value);
  
  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 32, "%u", jt->slrttp);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "jtslrttp", jsp.js_value);
  
  /* 暖气 */
  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 32, "%u", jt->nqfq);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "jtnqfq", jsp.js_value);
  
  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 32, "%d", jt->nqhitp);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "jtnqhitp", jsp.js_value);
  
  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 32, "%d", jt->nqlotp);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "jtnqlotp", jsp.js_value);
  
  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 32, "%u", jt->nqrttp);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "jtnqrttp", jsp.js_value);
  
  /* 报警 */
  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 32, "%u", jt->bjgw);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "jtbjgw", jsp.js_value);
  
  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 32, "%u", jt->bjdw);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "jtbjdw", jsp.js_value);
  
  /* 小窗同步风机开合度 */
  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 32, "%u", jt->xcfnins);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "xckhd", jsp.js_value);
  
  
  jsp.identity = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.identity, "productID", Route_str_pids[RT_DV_HK]);
  cJSON_AddStringToObject(jsp.identity, "deviceName", Route_DeviceList.hk.did);
  
  jsp.arrayitem = cJSON_CreateObject();
  cJSON_AddItemToObject(jsp.arrayitem, "identity", jsp.identity);
  cJSON_AddItemToObject(jsp.arrayitem, "properties", jsp.property);
  
  jsp.params = cJSON_CreateArray();
  cJSON_AddItemToArray(jsp.params, jsp.arrayitem);
  
  jsp.root = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.root, "id", "1");
  cJSON_AddStringToObject(jsp.root, "version", ONEJSON_VERSION);
  cJSON_AddItemToObject(jsp.root, "params", jsp.params);
  
  cJSON_PrintPreallocated(jsp.root, Network_Pub_payload, NETWORK_PUB_PAYLOAD_BUFFER_MAXLEN, 0);

	snprintf(Network_Pub_topic, NETWORK_PUB_TOPIC_BUFFER_MAXLEN, Route_str_Pub_Pack_Post, ROUTE_SELF_DID);
	
  cJSON_Delete(jsp.js_value);
  cJSON_Delete(jsp.root);
  cJSON_Delete(jsp.params);
  cJSON_Delete(jsp.arrayitem);
  cJSON_Delete(jsp.identity);
  cJSON_Delete(jsp.property);
  
  return 0;
}

static uint8_t route_ec_up_dl1(Route_PayloadType *rtpl)
{
  Ctrl_ParaDL1Type *dl1 = NULL;
  uint8_t i;
  uint8_t slen;
  
  if( (rtpl == NULL) || (rtpl->pl_len != sizeof(Ctrl_ParaDL1Type)) )
  {
    return 1;
  }
  
  dl1 = (Ctrl_ParaDL1Type*)(rtpl->payload);
  
  memset(&jsp, 0, sizeof(Network_JsonPointerType));
  memset(valuebuf, 0, sizeof(valuebuf));
  
  jsp.property = cJSON_CreateObject();
  
  /* 独立风机 */
  jsp.js_value = cJSON_CreateObject();
  slen = 0;
  for(i=0; i<13; i++)
  {
    slen += snprintf(valuebuf+slen, 9, "%d-", dl1->fntp[i]);
  }
  valuebuf[slen-1] = '\0';
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "dlfntp", jsp.js_value);
  
  /* 独立氺帘 */
  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 18, "%d-%d", dl1->sltp[0], dl1->sltp[1]);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "dlsltp", jsp.js_value);
  
  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 24, "%d-%d-%d", dl1->nqtp[0], dl1->nqtp[1], dl1->nqtp[2]);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "dlnqtp", jsp.js_value);
  
  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 36, "%d-%d-%d-%d", dl1->vftp[0], dl1->vftp[1], dl1->vftp[2], dl1->vftp[3]);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "dlvftp", jsp.js_value);
  
  
  jsp.identity = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.identity, "productID", Route_str_pids[RT_DV_HK]);
  cJSON_AddStringToObject(jsp.identity, "deviceName", Route_DeviceList.hk.did);
  
  jsp.arrayitem = cJSON_CreateObject();
  cJSON_AddItemToObject(jsp.arrayitem, "identity", jsp.identity);
  cJSON_AddItemToObject(jsp.arrayitem, "properties", jsp.property);
  
  jsp.params = cJSON_CreateArray();
  cJSON_AddItemToArray(jsp.params, jsp.arrayitem);
  
  jsp.root = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.root, "id", "1");
  cJSON_AddStringToObject(jsp.root, "version", ONEJSON_VERSION);
  cJSON_AddItemToObject(jsp.root, "params", jsp.params);
  
  cJSON_PrintPreallocated(jsp.root, Network_Pub_payload, NETWORK_PUB_PAYLOAD_BUFFER_MAXLEN, 0);

	snprintf(Network_Pub_topic, NETWORK_PUB_TOPIC_BUFFER_MAXLEN, Route_str_Pub_Pack_Post, ROUTE_SELF_DID);
	
  cJSON_Delete(jsp.js_value);
  cJSON_Delete(jsp.root);
  cJSON_Delete(jsp.params);
  cJSON_Delete(jsp.arrayitem);
  cJSON_Delete(jsp.identity);
  cJSON_Delete(jsp.property);
  
  return 0;
}

static uint8_t route_ec_up_dl2(Route_PayloadType *rtpl)
{
  Ctrl_ParaDL2Type *dl2 = NULL;
  
  if( (rtpl == NULL) || (rtpl->pl_len != sizeof(Ctrl_ParaDL2Type)) )
  {
    return 1;
  }
  
  dl2 = (Ctrl_ParaDL2Type*)(rtpl->payload);
  
  memset(&jsp, 0, sizeof(Network_JsonPointerType));
  memset(valuebuf, 0, sizeof(valuebuf));
  
  jsp.property = cJSON_CreateObject();
  
  /* 独立风机回差 */
  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 32, "%d", dl2->fnrttp);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "dlfnrt", jsp.js_value);
  
  /* 独立氺帘回差 */
  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 32, "%d", dl2->slrttp);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "dlslrt", jsp.js_value);
  
  /* 独立暖气分区控 */
  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 32, "%u", dl2->nqfq);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "dlnqfq", jsp.js_value);
  
  /* 独立暖气回差 */
  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 32, "%u", dl2->nqrttp);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "dlnqrt", jsp.js_value);
  
  
  jsp.identity = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.identity, "productID", Route_str_pids[RT_DV_HK]);
  cJSON_AddStringToObject(jsp.identity, "deviceName", Route_DeviceList.hk.did);
  
  jsp.arrayitem = cJSON_CreateObject();
  cJSON_AddItemToObject(jsp.arrayitem, "identity", jsp.identity);
  cJSON_AddItemToObject(jsp.arrayitem, "properties", jsp.property);
  
  jsp.params = cJSON_CreateArray();
  cJSON_AddItemToArray(jsp.params, jsp.arrayitem);
  
  jsp.root = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.root, "id", "1");
  cJSON_AddStringToObject(jsp.root, "version", ONEJSON_VERSION);
  cJSON_AddItemToObject(jsp.root, "params", jsp.params);
  
  cJSON_PrintPreallocated(jsp.root, Network_Pub_payload, NETWORK_PUB_PAYLOAD_BUFFER_MAXLEN, 0);

	snprintf(Network_Pub_topic, NETWORK_PUB_TOPIC_BUFFER_MAXLEN, Route_str_Pub_Pack_Post, ROUTE_SELF_DID);
	
  cJSON_Delete(jsp.js_value);
  cJSON_Delete(jsp.root);
  cJSON_Delete(jsp.params);
  cJSON_Delete(jsp.arrayitem);
  cJSON_Delete(jsp.identity);
  cJSON_Delete(jsp.property);
  
  return 0;
}

static uint8_t route_ec_up_sk(Route_PayloadType *rtpl)
{
  Ctrl_ParaSKType *sk = NULL;
  uint8_t i;
  
  if( (rtpl == NULL) || (rtpl->pl_len != sizeof(Ctrl_ParaSKType)) )
  {
    return 1;
  }
  
  sk = (Ctrl_ParaSKType*)(rtpl->payload);
  
  memset(&jsp, 0, sizeof(Network_JsonPointerType));
  memset(valuebuf, 0, sizeof(valuebuf));
  
  jsp.property = cJSON_CreateObject();
  
  /* 时控风机使能 */
  jsp.js_value = cJSON_CreateObject();
  valuebuf[1] = '\0';
  if(sk->fnen != 1)
  {
    valuebuf[0] = '0';
  }
  else
  {
    valuebuf[0] = '1';
  }
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "skfnen", jsp.js_value);
  
  /* 时控风机开启时间 */
  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 32, "%u", sk->fnkq);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "skfnkq", jsp.js_value);
  
  /* 时控风机关闭时间 */
  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 32, "%u", sk->fngb);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "skfngb", jsp.js_value);
  
  /* 时控风机通道 */
  for(i=0; i<13; i++)
  {
    if(sk->fntd[i] != 1)
    {
      valuebuf[i] = '0';
    }
    else
    {
      valuebuf[i] = '1';
    }
  }
  valuebuf[13] = '\0';
  jsp.js_value = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "skfntd", jsp.js_value);
  
  
  /* 时控氺帘使能 */
  jsp.js_value = cJSON_CreateObject();
  valuebuf[1] = '\0';
  if(sk->slen != 1)
  {
    valuebuf[0] = '0';
  }
  else
  {
    valuebuf[0] = '1';
  }
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "skslen", jsp.js_value);
  
  /* 时控氺帘开启时间 */
  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 32, "%u", sk->slkq);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "skslkq", jsp.js_value);
  
  /* 时控氺帘关闭时间 */
  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 32, "%u", sk->slgb);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "skslgb", jsp.js_value);
  
  /* 时控氺帘通道 */
  if(sk->sltd[0] != 1)
  {
    valuebuf[0] = '0';
  }
  else
  {
    valuebuf[0] = '1';
  }
  
  if(sk->sltd[1] != 1)
  {
    valuebuf[1] = '0';
  }
  else
  {
    valuebuf[1] = '1';
  }
  valuebuf[2] = '\0';
  jsp.js_value = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "sksltd", jsp.js_value);
  
  
  /* 时控暖气使能 */
  jsp.js_value = cJSON_CreateObject();
  valuebuf[1] = '\0';
  if(sk->nqen != 1)
  {
    valuebuf[0] = '0';
  }
  else
  {
    valuebuf[0] = '1';
  }
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "sknqen", jsp.js_value);
  
  /* 时控暖气开启时间 */
  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 32, "%u", sk->nqkq);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "sknqkq", jsp.js_value);
  
  /* 时控暖气关闭时间 */
  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 32, "%u", sk->nqgb);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "sknqgb", jsp.js_value);
  
  /* 时控暖气通道 */
  for(i=0; i<3; i++)
  {
    if(sk->nqtd[i] != 1)
    {
      valuebuf[i] = '0';
    }
    else
    {
      valuebuf[i] = '1';
    }
  }
  valuebuf[3] = '\0';
  jsp.js_value = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "sknqtd", jsp.js_value);
  
  
  /* 时控变频器使能 */
  jsp.js_value = cJSON_CreateObject();
  valuebuf[1] = '\0';
  if(sk->vfen != 1)
  {
    valuebuf[0] = '0';
  }
  else
  {
    valuebuf[0] = '1';
  }
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "skvfen", jsp.js_value);
  
  /* 时控变频器同步风机 */
  jsp.js_value = cJSON_CreateObject();
	snprintf(valuebuf, 32, "%u-%u-%u-%u", sk->vftf[0], sk->vftf[1], sk->vftf[2], sk->vftf[3]);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "skvftf", jsp.js_value);
  
  /* 时控变频器开启时间 */
  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 32, "%u-%u-%u-%u", sk->vfkq[0], sk->vfkq[1], sk->vfkq[2], sk->vfkq[3]);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "skvfkq", jsp.js_value);
  
  /* 时控变频器开启级数 */
  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 32, "%u-%u-%u-%u", sk->vfjs[0], sk->vfjs[1], sk->vfjs[2], sk->vfjs[3]);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "skvfjs", jsp.js_value);
  
  /* 时控变频器关闭时间 */
  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 32, "%u-%u-%u-%u", sk->vfgb[0], sk->vfgb[1], sk->vfgb[2], sk->vfgb[3]);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "skvfgb", jsp.js_value);
  
  
  jsp.identity = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.identity, "productID", Route_str_pids[RT_DV_HK]);
  cJSON_AddStringToObject(jsp.identity, "deviceName", Route_DeviceList.hk.did);
  
  jsp.arrayitem = cJSON_CreateObject();
  cJSON_AddItemToObject(jsp.arrayitem, "identity", jsp.identity);
  cJSON_AddItemToObject(jsp.arrayitem, "properties", jsp.property);
  
  jsp.params = cJSON_CreateArray();
  cJSON_AddItemToArray(jsp.params, jsp.arrayitem);
  
  jsp.root = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.root, "id", "1");
  cJSON_AddStringToObject(jsp.root, "version", ONEJSON_VERSION);
  cJSON_AddItemToObject(jsp.root, "params", jsp.params);
  
  cJSON_PrintPreallocated(jsp.root, Network_Pub_payload, NETWORK_PUB_PAYLOAD_BUFFER_MAXLEN, 0);

	snprintf(Network_Pub_topic, NETWORK_PUB_TOPIC_BUFFER_MAXLEN, Route_str_Pub_Pack_Post, ROUTE_SELF_DID);
  
  cJSON_Delete(jsp.js_value);
  cJSON_Delete(jsp.root);
  cJSON_Delete(jsp.params);
  cJSON_Delete(jsp.arrayitem);
  cJSON_Delete(jsp.identity);
  cJSON_Delete(jsp.property);
  
  return 0;
}

static uint8_t route_ec_up_td(Route_PayloadType *rtpl)
{
  Ctrl_ParaTDType *td = NULL;
  uint8_t i, pos;
  
  if( (rtpl == NULL) || (rtpl->pl_len != sizeof(Ctrl_ParaTDType)) )
  {
    return 1;
  }
  
  td = (Ctrl_ParaTDType*)(rtpl->payload);
  
  memset(&jsp, 0, sizeof(Network_JsonPointerType));
  memset(valuebuf, 0, sizeof(valuebuf));
   
  pos = 0;
  /* 风机通道 */
  for(i=0; i<13; i++)
  {
    if(td->fntd[i] != 1)
    {
      valuebuf[pos] = '0';
    }
    else
    {
      valuebuf[pos] = '1';
    }
    pos++;
  }
  
  valuebuf[pos++] = '-';
  
  /* 变频器通道 */
  for(i=0; i<4; i++)
  {
    if(td->vftd[i] != 1)
    {
      valuebuf[pos] = '0';
    }
    else
    {
      valuebuf[pos] = '1';
    }
    pos++;
  }
  
  valuebuf[pos++] = '-';
  
  /* 氺帘通道 */
  for(i=0; i<2; i++)
  {
    if(td->sltd[i] != 1)
    {
      valuebuf[pos] = '0';
    }
    else
    {
      valuebuf[pos] = '1';
    }
    pos++;
  }
  
  valuebuf[pos++] = '-';
  
  /* 暖气通道 */
  for(i=0; i<3; i++)
  {
    if(td->nqtd[i] != 1)
    {
      valuebuf[pos] = '0';
    }
    else
    {
      valuebuf[pos] = '1';
    }
    pos++;
  }
  
  valuebuf[pos++] = '-';
  
  /* 报警通道 */
  if(td->bjtd != 1)
  {
    valuebuf[pos] = '0';
  }
  else
  {
    valuebuf[pos] = '1';
  }
  
  pos++;
  valuebuf[pos++] = '-';
  
  if(td->zdtd != 1)
  {
    valuebuf[pos] = '0';
  }
  else
  {
    valuebuf[pos] = '1';
  }
  
  pos++;
  valuebuf[pos] = '\0';
  
  jsp.property = cJSON_CreateObject();
  
  jsp.js_value = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "tden", jsp.js_value);
  
  jsp.identity = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.identity, "productID", Route_str_pids[RT_DV_HK]);
  cJSON_AddStringToObject(jsp.identity, "deviceName", Route_DeviceList.hk.did);
  
  jsp.arrayitem = cJSON_CreateObject();
  cJSON_AddItemToObject(jsp.arrayitem, "identity", jsp.identity);
  cJSON_AddItemToObject(jsp.arrayitem, "properties", jsp.property);
  
  jsp.params = cJSON_CreateArray();
  cJSON_AddItemToArray(jsp.params, jsp.arrayitem);
  
  jsp.root = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.root, "id", "1");
  cJSON_AddStringToObject(jsp.root, "version", ONEJSON_VERSION);
  cJSON_AddItemToObject(jsp.root, "params", jsp.params);
  
  cJSON_PrintPreallocated(jsp.root, Network_Pub_payload, NETWORK_PUB_PAYLOAD_BUFFER_MAXLEN, 0);

	snprintf(Network_Pub_topic, NETWORK_PUB_TOPIC_BUFFER_MAXLEN, Route_str_Pub_Pack_Post, ROUTE_SELF_DID);
	
  cJSON_Delete(jsp.js_value);
  cJSON_Delete(jsp.root);
  cJSON_Delete(jsp.params);
  cJSON_Delete(jsp.arrayitem);
  cJSON_Delete(jsp.identity);
  cJSON_Delete(jsp.property);
  
  return 0;
}

uint8_t Route_ec_up(Route_PayloadType *rtpl)
{
  uint8_t err = 1;
  
  switch(rtpl->pl_type)
  {
		case ROUTE_PLTYP_EC_COLLECTOR:
			err = route_ec_up_collector(rtpl);
			break;
		
    case ROUTE_PLTYP_EC_STATE_FN:
			err = route_ec_up_st_fn(rtpl);
      break;
    
		case ROUTE_PLTYP_EC_STATE_SL:
			err = route_ec_up_st_sl(rtpl);
      break;
		
		case ROUTE_PLTYP_EC_STATE_NQ:
			err = route_ec_up_st_nq(rtpl);
      break;
		
		case ROUTE_PLTYP_EC_STATE_VF:
			err = route_ec_up_st_vf(rtpl);
      break;
		
    case ROUTE_PLTYP_EC_MD:
      err = route_ec_up_md(rtpl);
      break;
    
    case ROUTE_PLTYP_EC_JT:
      err = route_ec_up_jt(rtpl);
      break;
    
    case ROUTE_PLTYP_EC_DL1:
      err = route_ec_up_dl1(rtpl);
      break;
    
    case ROUTE_PLTYP_EC_DL2:
      err = route_ec_up_dl2(rtpl);
      break;
    
    case ROUTE_PLTYP_EC_SK:
      err = route_ec_up_sk(rtpl);
      break;
    
    case ROUTE_PLTYP_EC_TD:
      err = route_ec_up_td(rtpl);
      break;
    
    default:
      err = 1;
      break;
  }
  
  return err;
}
